package com.example.hao.flowercityfm.HFAdapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.hao.flowercityfm.R;
import com.example.hao.flowercityfm.pojo.Type1ListItem;

public class Type1PagerAdapter extends PagerAdapter {
    private Context context;

    public Type1PagerAdapter(Context context) {
        this.context = context;
    }

   Type1ListItem type1ListItem;

    public void setType1ListItem(Type1ListItem type1ListItem) {
        this.type1ListItem = type1ListItem;
        notifyDataSetChanged();
    }



    @Override
    public int getCount() {

        return type1ListItem == null? 0: type1ListItem.getData().size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        Type1ListItem.DataBean dataBean = type1ListItem.getData().get(position);
        String pictrueUrl = dataBean.getPictrueUrl();

        View view = LayoutInflater.from(context).inflate(R.layout.fragment_home_rotaion_item, container, false);
        ImageView iv_banner = view.findViewById(R.id.iv_bannerImage);
        Glide.with(context).load(pictrueUrl).into(iv_banner);
        container.addView(view);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "点击了第"+position+"广告图", Toast.LENGTH_SHORT).show();
            }
        });

        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }
}
